/******************************************************************************
 * @File name   :      MaintainCarPlateServiceImpl.java
 *
 * @Author      :      ANMA
 *
 * @Date        :      Jul 28, 2015
 *
 * @Copyright Notice: 
 * Copyright (c) 2014 DMS, Inc. All  Rights Reserved.
 * This software is published under the terms of the DMS Software
 * License version 1.0, a copy of which has been included with this
 * distribution in the LICENSE.txt file.
 * 
 * ----------------------------------------------------------------------------
 * Date                         Who         Version         Comments
 * Jul 28, 2015 2:09:29 PM    ANMA      1.0            Initial Version
 *****************************************************************************/
package com.cap.bts.crmdp.dealer.member.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cap.bts.crmdp.dealer.member.constants.MaintainMemberConstants;
import com.cap.bts.crmdp.dealer.member.dao.IMaintainCarPlateDao;
import com.cap.bts.crmdp.dealer.member.domain.CarPlateInfo;
import com.cap.bts.crmdp.dealer.member.dto.MemberResultsDto;
import com.cap.bts.crmdp.dealer.member.service.IMaintainCarPlateService;
import com.cap.bts.framework.codemapping.domain.CodeInfo;
import com.cap.bts.framework.codemapping.service.ICodeMapping;
import com.cap.bts.framework.common.exception.AppException;

/**
 * 
 */
@Transactional(rollbackFor = AppException.class)
@Service
public class MaintainCarPlateServiceImpl implements IMaintainCarPlateService {

	@Resource(name = "maintainCarPlateDao")
	private IMaintainCarPlateDao maintainCarPlateDao;
	
	@Autowired
	private ICodeMapping codeMapping;

	@Override
	public List<CarPlateInfo> getCarPlateInfoListByCarPlateOrMemberId(String carPlate, Long memberId) throws AppException {
		return maintainCarPlateDao.getCarPlateInfoListByCarPlateOrMemberId(carPlate, memberId);
	}

	@Override
	public CarPlateInfo saveCarPlateInfo(MemberResultsDto dto) throws AppException {
		CarPlateInfo info = new CarPlateInfo();
		info.setCarPlate(dto.getCarPlate());
		info.setMemberId(dto.getId());
		//此处需要从微信接口获取 注册日期  和 状态
		//注册日期（取微信注册日期，非微信用户取系统中的创建日期）
		info.setRegisterDate(new Date());
		//未激活，已激活
		info.setMemberStatus("0");

		return maintainCarPlateDao.save(info);
	}

    /**
     * {@inheritDoc} 
     * overridden:
     * @Date        :      2015年8月6日
     * @see com.cap.bts.crmdp.dealer.member.service.IMaintainCarPlateService#removeCarPlateInfo(java.lang.Long)
    **/
    @Override
    public boolean removeCarPlateInfo(Long id) throws AppException {
        try {
            maintainCarPlateDao.remove(id);
            return true;
        } catch (Exception e) {
            throw new AppException("removeCarPlateInfo", e);
        }
    }

    /**
     * {@inheritDoc} 
     * overridden:
     * @Date        :      2015年8月12日
     * @see com.cap.bts.crmdp.dealer.member.service.IMaintainCarPlateService#existUsingPackage(java.lang.Long)
    **/
    @Override
    public boolean existUsingPackage(Long id) throws AppException {
        try {
            int count = maintainCarPlateDao.getCountUsingPackageCarPlateId(id);
            if(count != 0){
                return true;
            }
            return false;
        } catch (Exception e) {
            throw new AppException("removeCarPlateInfo", e);
        }
    }

    /**
     * {@inheritDoc} 
     * overridden:
     * @Date        :      2015年8月17日
     * @see com.cap.bts.crmdp.dealer.member.service.IMaintainCarPlateService#getCarPlateInfoListByCarPlateAndMemberId(java.lang.String, java.lang.Long)
    **/
    @Override
    public List<CarPlateInfo> getCarPlateInfoListByCarPlateAndMemberId(String carPlate,
        Long memberId) throws AppException {
        return maintainCarPlateDao.getCarPlateInfoListByCarPlateAndMemberId(carPlate, memberId);
    }

    /**
     * {@inheritDoc} 
     * overridden:
     * @Date        :      2015年8月25日
     * @see com.cap.bts.crmdp.dealer.member.service.IMaintainCarPlateService#getCarPlateInfo(java.lang.Long, java.lang.String, java.lang.Long)
    **/
    @Override
    public CarPlateInfo getCarPlateListWithPackage(Long dealerId, Long carPlateId, Long memberId)
        throws AppException {
        List<CarPlateInfo> list = maintainCarPlateDao.getCarPlateListWithPackage(dealerId, carPlateId, memberId);
        CarPlateInfo carPlateInfo = null;
        if(list != null && !list.isEmpty()){
            carPlateInfo = list.get(0);
        }
        return carPlateInfo;
    }

    /**
     * {@inheritDoc} 
     * overridden:
     * @Date        :      2015年9月6日
     * @see com.cap.bts.crmdp.dealer.member.service.IMaintainCarPlateService#updateCarPlateByCarPlateInfo(com.cap.bts.crmdp.dealer.member.domain.CarPlateInfo)
    **/
    @Override
    public void updateCarPlateByCarPlateInfo(CarPlateInfo carPlateInfo, Long newMemberId) throws AppException {
        maintainCarPlateDao.updateCarPlateByCarPlateInfo(carPlateInfo,newMemberId);
    }

    /**
     * {@inheritDoc} 
     * overridden:
     * @return 
     * @Date        :      2015年9月6日
     * @see com.cap.bts.crmdp.dealer.member.service.IMaintainCarPlateService#updateCarPlateForMember(java.lang.Long, java.lang.String, int)
    **/
    @Override
    public boolean updateCarPlateForMember(MemberResultsDto memberResultsDto, int updateTimes) throws AppException {
        try {
            CodeInfo codeInfo = codeMapping.getCodeInfoByCode(MaintainMemberConstants.UPDATE_TIMES);
            if(codeInfo != null){
                if(Integer.parseInt(codeInfo.getValue()) >= updateTimes + 1){
                    maintainCarPlateDao.updateCarPlateForMember(memberResultsDto.getId(), memberResultsDto.getCarPlate(), updateTimes + 1);
                    return true;
                }else{
                    memberResultsDto.setGreaterThanUpdateCarplateTimes(true);
                }
            }
        } catch (Exception e) {
            throw new AppException("updateCarPlateForMemberService", e);
        }
        return false;
    }

    /**
     * {@inheritDoc} 
     * overridden:
     * @Date        :      2015年9月6日
     * @see com.cap.bts.crmdp.dealer.member.service.IMaintainCarPlateService#getCarPlateInfoById(java.lang.Long)
    **/
    @Override
    public CarPlateInfo getCarPlateInfoById(Long id) throws AppException {
        return maintainCarPlateDao.getCarPlateInfoById(id);
    }

    /**
     * {@inheritDoc} 
     * overridden:
     * @Date        :      2015年9月28日
     * @see com.cap.bts.crmdp.dealer.member.service.IMaintainCarPlateService#updateCarPlate(com.cap.bts.crmdp.dealer.member.domain.CarPlateInfo, java.lang.Long)
    **/
    @Override
    public void updateCarPlate(CarPlateInfo carPlateInfo) throws AppException {
        maintainCarPlateDao.updateCarPlate(carPlateInfo);
    }

    /**
     * {@inheritDoc} 
     * overridden:
     * @Date        :      2015年10月15日
     * @see com.cap.bts.crmdp.dealer.member.service.IMaintainCarPlateService#getCarPlateInfoListByCarPlate(java.lang.String)
    **/
    @Override
    public List<CarPlateInfo> getCarPlateInfoListByCarPlate(String carPlate) throws AppException {
        return maintainCarPlateDao.getCarPlateInfoListByCarPlate(carPlate);
    }

	@Override
	public CarPlateInfo getCarPlateInfoByMarsCarId(String carId) {
		return maintainCarPlateDao.getCarPlateInfoByMarsCarId(carId);
	}

    /**
     * {@inheritDoc} 
     * overridden:
     * @Date        :      2015年11月25日
     * @see com.cap.bts.crmdp.dealer.member.service.IMaintainCarPlateService#saveWechatCarPlateInfo(com.cap.bts.crmdp.dealer.member.domain.CarPlateInfo)
    **/
    @Override
    public CarPlateInfo saveWechatCarPlateInfo(CarPlateInfo carPlateInfo) throws AppException {
        return maintainCarPlateDao.save(carPlateInfo);
    }
    
    
}
